package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接,并设置连接信息
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672); //可选,5672是默认端口
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();//通信的通道
        //2.定义交换机，命名成logs
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);//默认非持久交换机

        //3.logs交换机发送消息
        while (true){
            System.out.println("输入：");
            String msg = new Scanner(System.in).nextLine();
            //第二个参数，对fanout交换机无效
            c.basicPublish("logs", "", null, msg.getBytes());
        }

    }
}
